package com.qiaofang.collectmanage.facade.impl;

import java.util.List;
import javax.validation.Valid;

import com.qiaofang.collectmanage.stub.request.*;
import com.qiaofang.collectmanage.stub.response.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.qiaofang.common.constant.CommonResponseCode;
import com.qiaofang.collectmanage.stub.constants.CrawlerTaskResponseCode;
import com.qiaofang.collectmanage.service.CrawlerTaskService;
import com.qiaofang.collectmanage.stub.bean.CrawlerTaskDTO;
import com.qiaofang.collectmanage.stub.facade.CrawlerTaskFacade;

import com.qiaofang.collectmanage.stub.request.RemoveCrawlerTaskRequest;

/**
* Created by generator on 2018-8-1 13:49:21
*/
@RestController
public class CrawlerTaskFacadeImpl implements CrawlerTaskFacade {


    @Autowired
    private CrawlerTaskService taskService;


   @Override
    public GetCrawlerTaskByUuidResponse getCrawlerTaskByUuid(@RequestBody @Valid GetCrawlerTaskByUuidRequest request){
       GetCrawlerTaskByUuidResponse response = new GetCrawlerTaskByUuidResponse();
       CrawlerTaskDTO dto = taskService.getCrawlerTaskByUuid(request.getUuid());
       response.setTaskDTO(dto);
       response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
       response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
      return response;
    }
    @Override
    public GetCrawlerTaskByTaskResponse getCrawlerTaskByTask(@RequestBody @Valid GetCrawlerTaskByTaskRequest request){
        GetCrawlerTaskByTaskResponse response = new GetCrawlerTaskByTaskResponse();
        CrawlerTaskDTO dto = taskService.getCrawlerTaskByTask(request.getTaskDTO());
        response.setTaskDTO(dto);
        response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
        response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
        return response;
    }

    @Override
    public SaveCrawlerTaskResponse saveCrawlerTask(@RequestBody @Valid SaveCrawlerTaskRequest request){
        SaveCrawlerTaskResponse response = new SaveCrawlerTaskResponse();
        int count = taskService.saveCrawlerTask(request.getTaskDTO());
        if(count <= 0){
            response.setResponseCode(CrawlerTaskResponseCode.RC_SAVE_ERROR.getResponseCode());
            response.setResponseMessage(CrawlerTaskResponseCode.RC_SAVE_ERROR.getResponseMessage());
         }
        return response;
    }
    @Override
    public UpdateCrawlerTaskResponse updateCrawlerTask(@RequestBody @Valid UpdateCrawlerTaskRequest request){
        UpdateCrawlerTaskResponse response = new UpdateCrawlerTaskResponse();
        int count = taskService.updateCrawlerTask(request.getTaskDTO());
        if(count <= 0){
            response.setResponseCode(CrawlerTaskResponseCode.RC_UPDATE_ERROR.getResponseCode());
            response.setResponseMessage(CrawlerTaskResponseCode.RC_UPDATE_ERROR.getResponseMessage());
        }
        return response;
    }

    @Override
    public RemoveCrawlerTaskResponse removeCrawlerTask (@RequestBody @Valid RemoveCrawlerTaskRequest request){
        RemoveCrawlerTaskResponse response = new RemoveCrawlerTaskResponse();
        int count = taskService.removeCrawlerTaskByUuid(request.getUuid());
        if(count <= 0){
            response.setResponseCode(CrawlerTaskResponseCode.RC_REMOVE_ERROR.getResponseCode());
            response.setResponseMessage(CrawlerTaskResponseCode.RC_REMOVE_ERROR.getResponseMessage());
        }
        return response;
    }
    @Override
    public SearchCrawlerTaskResponse searchCrawlerTask (@RequestBody @Valid SearchCrawlerTaskRequest request){
        SearchCrawlerTaskResponse response = new SearchCrawlerTaskResponse();
        Long count = taskService.countCrawlerTaskByPermission(request.getSearchTaskDTO());
        if(count > 0){
          response.setList(taskService.searchCrawlerTaskByPermission(request.getSearchTaskDTO(),request.getPageDTO()));
        }
        response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
        response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
        return response;
    }
    @Override
    public ListCrawlerTaskResponse listCrawlerTask (@RequestBody @Valid ListCrawlerTaskRequest request){
         ListCrawlerTaskResponse response = new ListCrawlerTaskResponse();
         List<CrawlerTaskDTO> data = taskService.listCrawlerTask(request.getTaskDTO());
         response.setList(data);
         response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
         response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
         return response;
    }


   @Override
    public ListCrawlerTaskByUuidsResponse listCrawlerTaskByUuids (@RequestBody @Valid ListCrawlerTaskByUuidsRequest request){
        ListCrawlerTaskByUuidsResponse response = new ListCrawlerTaskByUuidsResponse();
        List<CrawlerTaskDTO> data = taskService.listCrawlerTaskByUuids(request.getTaskUuidList());
        response.setList(data);
        response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
        response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
        return response;
    }


    /**
     * 通过ids获取爬虫任务
     * @param request
     * @return
     */
    @Override
    public ListCollectRuleByIdsResponse listCrawlerTaskCollectRuleByIds(@RequestBody @Valid ListCollectRuleByIdsRequest request) {
        ListCollectRuleByIdsResponse  listCollectRuleByIdsResponse = new ListCollectRuleByIdsResponse();
        listCollectRuleByIdsResponse.setData(taskService.listCrawlerCollectRuleByIds(request.getIds()));
        return listCollectRuleByIdsResponse;
    }
}
